/*
 * @lc app=leetcode.cn id=322 lang=javascript
 *
 * [322] 零钱兑换
 */

// @lc code=start
/**
 * @param {number[]} coins
 * @param {number} amount
 * @return {number}
 */
// f(i) : 总金额为 i ，需要的最少数量
var coinChange = function(coins, amount) {
  let m = coins.length;
  let f = new Array(amount + 1);
  for (let  i = 1; i <= amount; i++) f[i] = -1;

  f[0] = 0;
  for (let i = 1; i <= amount; i++) {
    for (let j = 0; j < m; j++) {
      if(coins[j] > i) continue;
      if(f[i - coins[j]] == -1) continue;
      if(f[i] === -1 || f[i] > f[i - coins[j]] + 1) f[i] = f[i - coins[j]] + 1;
    }
  }
  return f[amount];
};
// @lc code=end

